﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MotionController))]
public class InputController : MonoBehaviour
{
    public QKEvent InputDelegate = new QKEvent();

    public float SendInterval = 0.05f;
    private float TimeCount = 0;

    private Direction CurrentDirection = Direction.None;

    void Update()
    {
        Direction direction = Direction.None;

        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
            direction = Direction.Left;
        else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
            direction = Direction.Up;
        else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
            direction = Direction.Right;
        else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
            direction = Direction.Down;

        if (direction != Direction.None)
        {
            TimeCount += Time.deltaTime;
            if (TimeCount >= SendInterval)
            {
                if (InputDelegate != null)
                {
                    InputDelegate.Call(new object[] { (int)direction });
                }
                TimeCount = 0;
            }
        }
        else if (CurrentDirection != Direction.None)
        {
            if (InputDelegate != null)
            {
                InputDelegate.Call(new object[] { (int)direction });
            }
        }
        CurrentDirection = direction;
    }
}
